<template>
  <div class="app">
    <TodoHeader @add="add"></TodoHeader>
    <TodoMain :list="list" @del="del"></TodoMain>
    <TodoFooter :list="list" @clear="clear"></TodoFooter>
  </div>
</template>
<script>
import TodoHeader from "@/jishiben/TodoHeaders.vue";
import TodoFooter from "@/jishiben/TodoFooter.vue";
import TodoMain from "@/jishiben/TodoMain.vue";
export default {
  components: {
    TodoHeader,
    TodoFooter,
    TodoMain,
  },
  data() {
    return {
      //   list: [
      //     { id: 1, content: "倒立跑步一公里" },
      //     { id: 2, content: "游泳10000000米" },
      //     { id: 3, content: "打球10000000次" },
      //     { id: 4, content: "跳高10000000米" },
      //   ],
      list: JSON.parse(localStorage.getItem("list")) || [
        { id: 1, content: "倒立跑步一公里" },
        { id: 2, content: "游泳10000000米" },
        { id: 3, content: "打球10000000次" },
        { id: 4, content: "跳高10000000米" },
      ],
    };
  },
  watch: {
    list: {
      deep: true,
      handler(newVal) {
        localStorage.setItem("list", JSON.stringify(this.list));
      },
    },
  },
  computed: {},
  methods: {
    clear() {
      this.list = [];
    },
    add(content) {
      this.list.push({ id: Date.now(), content });
    },
    del(index) {
      this.list.splice(index, 1);
    },
  },
};
</script>
<style lang='less'  scoped>
</style>
